package com.lcw.library.imagepicker.manager;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lcw.library.imagepicker.utils.PermissionUtil;

/**
 * 权限请求Fragment
 * 
 * 用于处理权限请求的隐藏Fragment
 * 避免在Activity中直接处理权限请求
 * 支持所有Android版本的权限处理
 * 
 */
public class PermissionFragment extends Fragment {

    private static final String KEY_PERMISSION_MANAGER = "permission_manager";
    private PermissionManager permissionManager;

    public static PermissionFragment newInstance(PermissionManager permissionManager) {
        PermissionFragment fragment = new PermissionFragment();
        fragment.permissionManager = permissionManager;
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 请求权限
        if (permissionManager != null) {
            PermissionUtil.requestStoragePermission(this, PermissionManager.getRequestCode());
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        if (permissionManager != null) {
            permissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        
        // 移除Fragment
        if (getFragmentManager() != null) {
            getFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
        }
    }
} 